package com.zz.parkingsystem.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.zz.parkingsystem.entity.Inspector;
import com.zz.parkingsystem.mapper.InspectorMapper;
import com.zz.parkingsystem.service.InspectorService;
import com.zz.parkingsystem.util.MD5Util;
import com.zz.parkingsystem.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Auther: BAISIHANG
 * @Date: 2022/8/30 - 08 - 30 - 14:21
 * @Description: com.zz.parkingsystem.service.impl
 * @version: 1.0
 */
@Service
public class InspectorServiceImpl implements InspectorService {
    @Resource
    private InspectorMapper inspectorMapper;

    @Override
    public Inspector getselectByPrimaryTel(String ins_tel) {
        return inspectorMapper.selectByPrimaryTel(ins_tel);
    }

    @Override
    public Inspector getselectByPrimaryByTel(String insTel, String insPassword) {
        return inspectorMapper.selectByPrimaryBytelAndpwd(insTel,insPassword);
    }

    @Override
    public Inspector getselectByPrimaryByTel(String insTel) {
        return inspectorMapper.selectByPrimaryByTel(insTel);
    }

    @Override
    public int add(Inspector inspector) {
        Inspector inspectorResult = inspectorMapper.selectByPrimaryByTel(inspector.getInsTel());
        if(ObjectUtil.isNull(inspectorResult)){
            Inspector inspector1 = new Inspector();
            String salt = RandomUtil.randomNumbers(6);
            String passwordMD5= MD5Util.MD5Encode(inspector.getInsPassword(), salt, "UTF-8");
            inspector1.setInsPassword(passwordMD5);
            inspector1.setInsTel(inspector.getInsTel());
            inspector1.setInsSalt(salt);
            inspector1.setAdminId(1);
            return inspectorMapper.insertSelective(inspector1);
        }else
            return -1;

    }

    @Override
    public int getselAllsum() {
        return inspectorMapper.selAllsum();
    }

    @Override
    public boolean login(String insTel, String insPassword) {
        if (NumberUtil.isPhone(insTel)) {
            Inspector inspectorResult = inspectorMapper.selectByPrimaryByTel(insTel);
            String salt = inspectorResult.getInsSalt();
            String inputPwd = MD5Util.MD5Encode(insPassword, salt, "UTF-8");
            if (inputPwd.equals(inspectorResult.getInsPassword())){
                return true;
            }

        }
        return false;
    }


}
